﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Text;

namespace HTTPGetUtility
{
    /// <summary>    
    /// Client message inspector used to intercept and manipulate the     
    /// SOAP message being sent to the target service. Will set the    
    /// request verb to 'GET' and suppress the message body.    
    /// </summary>    

    public class HttpGetClientMessageInspector : IClientMessageInspector
    {
        #region Constructors
        /// <summary>    
        /// Default constructor    
        /// </summary>    
        public HttpGetClientMessageInspector()
        {

        }

        #endregion

        #region IClientMessageInspector Members

        /// <summary>    
        /// This method intercepts the request message being sent to the target service.    
        /// </summary>    
        /// <param name="request">The request SOAP message being sent to the target service</param>    
        /// <param name="channel">The WCF client object channel</param>    
        /// <returns>An object that can be used for correlation with the response</returns>    
        /// <remarks>    
 
        /// Used to set the request verb to 'GET' and suppress the message body.    
        /// Correlation is not required.    
        /// </remarks>    
        
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            // Access HTTP request properties    
            HttpRequestMessageProperty messageProperty;
            if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
            {
                messageProperty = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
            }
            else
            {
                messageProperty = new HttpRequestMessageProperty();
                request.Properties.Add(HttpRequestMessageProperty.Name, messageProperty);
            }

            // Set the method verb to 'GET' and exclude the message body    
            messageProperty.Method = "GET";
            messageProperty.SuppressEntityBody = true;
           
            // Create a new non-SOAP request message    
            Message message = Message.CreateMessage(MessageVersion.None, "*");
            message.Properties.Add(HttpRequestMessageProperty.Name, messageProperty);
            request = message;

            // No correlation required    
            return null;
        }

        /// <summary>    
        /// This method intercepts the SOAP response message returned by the target service.    
        /// </summary>    
        /// <param name="reply">SOAP response message returned by the target service.</param>    
        /// <param name="correlationState">Correlation state data.</param>    
        /// <remarks>Not currently used.</remarks>    

        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            Message temp = Message.CreateMessage(reply.Version, reply.Headers.Action,
            reply.GetReaderAtBodyContents());
            MessageBuffer buffer = temp.CreateBufferedCopy(int.MaxValue);
            reply = buffer.CreateMessage();
        } 
    }
        #endregion
}


